package com.qf.service.impl;


import com.qf.constans.ResultCode;
import com.qf.dto.UserDto;
import com.qf.exceptions.AddDataException;
import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void addUser(UserDto userDto) {
        if (userDto==null || userDto.isNull()){
            throw new AddDataException("数据不能为空!", ResultCode.USERNAME_OR_PASSWORD_IS_ERROR);
        }
        if (!userDto.getPassword().equals(userDto.getRepeatpassword())){
            throw new AddDataException("两次密码输入不同!!!", ResultCode.PASSWORD_NOT_SAME);
        }

        userMapper.addUser(userDto);

    }

    @Override
    public User login(UserDto userDto) {
        User user = userMapper.getUserByUserName(userDto.getUsername());
        if (userDto.getPassword().equals(user.getPassword())){
            return user;
        }
        return null;
    }
}



